package com.example.weathermvvm.data.db

import com.example.weathermvvm.data.model.place.City
import com.example.weathermvvm.data.model.place.County
import com.example.weathermvvm.data.model.place.Province
import org.litepal.LitePal

/**
 *
 * @Author： LJH
 * @Time： 2022/8/10
 * @description：Room三个主要组件：数据实体、数据访问对象(DAO)、数据库类
 * 数据访问对象（DAO）提供应用在数据库中检索、更新、插入和删除数据所用的方法
 */
class PlaceDao {

    fun getProvinces():MutableList<Province> = LitePal.findAll(Province::class.java)

    fun saveProvinceList(provinces:List<Province>?){
        if (provinces != null && provinces.isNotEmpty()){
            LitePal.saveAll(provinces)
        }
    }

    fun getCities(provinceId:Int):MutableList<City> = LitePal.where("provinceId = ?",provinceId.toString()).find(City::class.java)

    fun saveCityList(cityList:List<City>?){
        if (cityList != null && cityList.isNotEmpty()){
            LitePal.saveAll(cityList)
        }
    }

    fun getCounties(cityCode:Int):MutableList<County> = LitePal.where("cityId = ?",cityCode.toString()).find(County::class.java)

    fun saveCountyList(counties:List<County>?){
        if (counties != null && counties.isNotEmpty()){
            LitePal.saveAll(counties)
        }
    }
}